home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap17 / EZTest / FontDemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.8 KB  |  154 lines

  1. /*------------------------------------------------
  2.    FONTDEMO.C -- Font Demonstration Shell Program
  3.                  (c) Charles Petzold, 1998
  4.   ------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "..\\EZTest\\EzFont.h"
  8. #include "..\\EZTest\\resource.h"
  9.  
  10. extern  void     PaintRoutine (HWND, HDC, int, int) ;
  11. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  12.  
  13. HINSTANCE hInst ;
  14.  
  15. extern TCHAR szAppName [] ;
  16. extern TCHAR szTitle [] ;
  17.  
  18. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  19.                     PSTR szCmdLine, int iCmdShow)
  20. {
  21.      TCHAR    szResource [] = TEXT ("FontDemo") ;
  22.      HWND     hwnd ;
  23.      MSG      msg ;
  24.      WNDCLASS wndclass ;
  25.      
  26.      hInst = hInstance ;
  27.      
  28.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  29.      wndclass.lpfnWndProc   = WndProc ;
  30.      wndclass.cbClsExtra    = 0 ;
  31.      wndclass.cbWndExtra    = 0 ;
  32.      wndclass.hInstance     = hInstance ;
  33.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  34.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  35.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  36.      wndclass.lpszMenuName  = szResource ;
  37.      wndclass.lpszClassName = szAppName ;
  38.      
  39.      if (!RegisterClass (&wndclass))
  40.      {
  41.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  42.                       szAppName, MB_ICONERROR) ;
  43.           return 0 ;
  44.      }
  45.      
  46.      hwnd = CreateWindow (szAppName, szTitle,
  47.                           WS_OVERLAPPEDWINDOW,
  48.                           CW_USEDEFAULT, CW_USEDEFAULT,
  49.                           CW_USEDEFAULT, CW_USEDEFAULT,
  50.                           NULL, NULL, hInstance, NULL) ;
  51.      
  52.      ShowWindow (hwnd, iCmdShow) ;
  53.      UpdateWindow (hwnd) ;
  54.      
  55.      while (GetMessage (&msg, NULL, 0, 0))
  56.      {
  57.           TranslateMessage (&msg) ;
  58.           DispatchMessage (&msg) ;
  59.      }
  60.      return msg.wParam ;
  61. }
  62.  
  63. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  64. {
  65.      static DOCINFO  di = { sizeof (DOCINFO), TEXT ("Font Demo: Printing") } ;
  66.      static int      cxClient, cyClient ;
  67.      static PRINTDLG pd = { sizeof (PRINTDLG) } ;
  68.      BOOL            fSuccess ;
  69.      HDC             hdc, hdcPrn ;
  70.      int             cxPage, cyPage ;
  71.      PAINTSTRUCT     ps ;
  72.      
  73.      switch (message)
  74.      {
  75.      case WM_COMMAND:
  76.           switch (wParam)
  77.           {
  78.           case IDM_PRINT:
  79.  
  80.                     // Get printer DC
  81.  
  82.                pd.hwndOwner = hwnd ;
  83.               pd.Flags     = PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION ;
  84.  
  85.               if (!PrintDlg (&pd))
  86.                     return 0 ;
  87.  
  88.                if (NULL == (hdcPrn = pd.hDC))
  89.                {
  90.                     MessageBox (hwnd, TEXT ("Cannot obtain Printer DC"),
  91.                                 szAppName, MB_ICONEXCLAMATION | MB_OK) ;
  92.                     return 0 ;
  93.                }
  94.                     // Get size of printable area of page
  95.  
  96.                cxPage = GetDeviceCaps (hdcPrn, HORZRES) ;
  97.                cyPage = GetDeviceCaps (hdcPrn, VERTRES) ;
  98.  
  99.                fSuccess = FALSE ;
  100.  
  101.                     // Do the printer page
  102.  
  103.                SetCursor (LoadCursor (NULL, IDC_WAIT)) ;
  104.                ShowCursor (TRUE) ;
  105.  
  106.                if ((StartDoc (hdcPrn, &di) > 0) && (StartPage (hdcPrn) > 0))
  107.                {
  108.                     PaintRoutine (hwnd, hdcPrn, cxPage, cyPage) ;
  109.                     
  110.                     if (EndPage (hdcPrn) > 0)
  111.                     {
  112.                          fSuccess = TRUE ;
  113.                          EndDoc (hdcPrn) ;
  114.                     }
  115.                }
  116.                DeleteDC (hdcPrn) ;
  117.  
  118.                ShowCursor (FALSE) ;
  119.                SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
  120.  
  121.                if (!fSuccess)
  122.                     MessageBox (hwnd, 
  123.                                 TEXT ("Error encountered during printing"),
  124.                                 szAppName, MB_ICONEXCLAMATION | MB_OK) ;
  125.                return 0 ;
  126.  
  127.           case IDM_ABOUT:
  128.                MessageBox (hwnd, TEXT ("Font Demonstration Program\n")
  129.                                  TEXT ("(c) Charles Petzold, 1998"),
  130.                            szAppName, MB_ICONINFORMATION | MB_OK);
  131.                return 0 ;
  132.           }
  133.           break ;
  134.           
  135.      case WM_SIZE:
  136.           cxClient = LOWORD (lParam) ;
  137.           cyClient = HIWORD (lParam) ;
  138.           return 0 ;
  139.           
  140.      case WM_PAINT:
  141.           hdc = BeginPaint (hwnd, &ps) ;
  142.           
  143.           PaintRoutine (hwnd, hdc, cxClient, cyClient) ;
  144.           
  145.           EndPaint (hwnd, &ps) ;
  146.           return 0 ;
  147.           
  148.      case WM_DESTROY :
  149.           PostQuitMessage (0) ;
  150.           return 0 ;
  151.      }
  152.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  153. }
  154.